<?php

namespace App\Admin\Controllers;

use App\NavigationList;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;

class NavigationListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '导航';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new NavigationList);
        $grid->disableExport();
        $navigation_type = DB::table('navigation_type')
            ->where('state', 1)
            ->pluck('name', 'id');
        $grid->filter(function($filter) use ($navigation_type){
            // 在这里添加字段过滤器
            $filter->like('name', '名称');
            $filter->equal('navigation_type', '导航类别')->select($navigation_type);
            $filter->equal('state', '状态')->select(['关闭', '打开']);
        });
        $grid->disableExport();
        $grid->actions(function ($actions) {
            $actions->disableView();
        });
        $grid->model()->orderBy('id', 'desc');
        $grid->column('id', __('Id'))->sortable();
        $grid->column('image', '封面图')->image('', 80, 80);
        $grid->column('name', '名称')->editable();
        $grid->column('description', '说明')->editable();
        $grid->column('url', '链接')->editable();
        $grid->column('order', '排序')->editable()->sortable();
        $grid->column('navigation_type', '导航分类')->select($navigation_type);
        $state = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $grid->column('state', '是否展示')->switch($state)->sortable();
        $grid->column('is_login', '登录验证')->switch($state)->sortable();
        $grid->column('created_at', '创建时间')->sortable();
        $grid->column('updated_at', '更新时间')->sortable();

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(NavigationList::findOrFail($id));

        $show->field('id', __('Id'));
        $show->field('navigation_type', __('Navigation type'));
        $show->field('name', __('Name'));
        $show->field('image', __('Image'));
        $show->field('description', __('Description'));
        $show->field('url', __('Url'));
        $show->field('state', __('State'));
        $show->field('order', __('Order'));
        $show->field('created_at', __('Created at'));
        $show->field('updated_at', __('Updated at'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new NavigationList);
        $navigation_type = DB::table('navigation_type')
            ->where('state', 1)
            ->pluck('name', 'id');
        $state = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $form->select('navigation_type', '导航分类')->options($navigation_type);
        $form->text('name', '名称');
        $form->image('image', '封面');
        $form->text('description', '说明');
        $form->text('url', '链接地址');
        $form->switch('state', '是否展示')->states($state)->default(1);
        $form->switch('is_login', '登录验证')->states($state)->default(0);
        $form->number('order', '排序')->default(5);
        $form->saved(function (Form $form){
            $data = $form->model();
            $image = DB::table('navigation_list')->where('id', $data->id)->value('image');
            if(strpos($image,'http') === false){
                $img = isset($data->image)?env('APP_URL').'/upload/'.$data->image:false;
                $form->model()->where('id',$form->model()->id)->update(['image'=> $img]);
            }
        });
        return $form;
    }
}
